<?php
class userController extends Controller
{
    function showIndex()
    {

        $this->putIntoPage("user", $this->modules->User->load($this->getCredentials("userId")));
        $this->putIntoPage("myActivities", $this->modules->Activity->findRelatedCommentByUserId($this->getCredentials("userId")));
        $this->putIntoPage("myNews", $this->modules->News->findRelatedCommentByUserId($this->getCredentials("userId")));
        $this->render("profile");
        // TODO: Implement showIndex() method.
    }


    function addApplication()
    {
        $userId = $this->getCredentials("userId");
        $application = $this->modules->Application;
        if ($this->checkIdFromPost('applicationId')) {
            $this->modules->Application->update($this->_post);
            $sql = "update user set avatar = '" . $this->_post['photo'] . "' where userId=" . $this->getCredentials("userId");
            $this->modules->User->query($sql);
        }
        else {
            if (!$application->isApped($userId, 1)) {
                $this->modules->Application->insert($this->_post);
                $sql = "update user set avatar = '" . $this->_post['photo'] . "' where userId=" . $this->getCredentials("userId");
                $this->modules->User->query($sql);
            }
        }
        $this->baoming();
    }

    function saveProfile()
    {
        $this->modules->User->update($this->_post);
        $sql = "update applications set photo = '" . $this->_post['avatar'] . "' where userId=" . $this->getCredentials("userId");
        $this->modules->User->query($sql);
        $this->showIndex();
    }

    function savePassword()
    {
        $userId = $this->getCredentials("userId");
        $user = $this->modules->User->load($userId);
        if ($this->_post['oldPassword'] != "" && $this->_post['newPassword'] != "" && md5($this->_post['oldPassword'] . Util::$_passwordHash) == $user['password']) {
            if ($this->modules->User->resetPassword($user['userName'], $this->_post['newPassword'])) {
                $this->putIntoPage("message", "密码更新成功");
            }
            else {
                $this->putIntoPage("message", "密码更新失败");
            }
        } else {
            $this->putIntoPage("message", "新密码或则老密码输入错误");
        }

        $this->showIndex();
    }

    function baoming()
    {
        $this->putIntoPage("user", $this->modules->User->load($this->getCredentials("userId")));
        $this->putIntoPage("application", $this->modules->Application->loadMyCurrentVoteApplication($this->getCredentials("userId"), 1));
        $this->render("front_baoming");
    }
}

?>